package com.qfedu.mbtiapi.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.qfedu.mbtiapi.dao.QuestionDao;
import com.qfedu.mbtiapi.dao.UserTestDao;
import com.qfedu.mbtiapi.entity.Question;
import com.qfedu.mbtiapi.entity.UserTest;
import com.qfedu.mbtiapi.service.UserTestService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户测试表(UserTest)表服务实现类
 *
 * @author makejava
 * @since 2024-11-01 10:53:41
 */
@Service("userTestService")
public class UserTestServiceImpl implements UserTestService {
    @Resource
    private UserTestDao dao;
    @Resource
    private QuestionDao questionDao;

    private final String KEY="c57a9edaf005e0377594d31b71382041";

    /**
     * 创建测试
     * @param uid 用户ID*/
    @Override
    public String startTest(Integer uid) {
        List<Question> questions=new ArrayList<>();
        //1.准备参数
        Map<String,Object> param=new HashMap<>();
        param.put("key",KEY);

        //2.发起请求 获取响应
        String json= HttpUtil.get("http://apis.juhe.cn/fapig/character_test/questions",param);
        if(StringUtils.hasLength(json)) {
            //3.新增数据
            UserTest test = new UserTest(uid, "参加MBTI人格测试");
            dao.insert(test);

            //4.解析结果 获取本次对应的试题
            JSONArray array = new JSONObject(json).getJSONArray("result");
            for (int i = 0; i < array.size(); i++) {
                JSONObject jo = array.getJSONObject(i);
                questions.add(new Question(uid, "MBTI", test.getId(), jo.getStr("q"), jo.getStr("a"), jo.getStr("b"),
                        jo.getStr("ia"), jo.getStr("ib")));
            }
            //批量新增试题
            questionDao.insertBatch(questions);

            return "开始测试成功："+test.getId();
        }
        return "亲，开始测试失败！";
    }
    /**
     * 结束测试
     * @param id 测试ID*/
    @Override
    public String endTest(Integer id) {
        //1.查询对应的测试
        UserTest test=dao.selectById(id);

        //2.查询 测试对应的试题
        List<Question> questions=questionDao.selectByUtid(id);
        //3.遍历试题 获取用户的回答的内容 按照格式进行拼接
        StringBuffer answers=new StringBuffer();
        for (Question q:questions){
            answers.append(q.getAnswer()+",");
        }
        answers.deleteCharAt(answers.length()-1);

        //4.准备参数
        Map<String,Object> param=new HashMap<>();
        param.put("key",KEY);
        param.put("answers",answers.toString());

        //5.发起请求 获取响应
        String json=HttpUtil.get("http://apis.juhe.cn/fapig/character_test/analysis",param);
        if(StringUtils.hasLength(json)){
            try {
                //6.解析结果  获取的对应的数据
                JSONObject jo1 = new JSONObject(json);
                JSONObject jo2 = jo1.getJSONObject("result");
                test.setAlphabet(jo2.getStr("alphabet"));
                test.setVocabulary(jo2.getStr("vocabulary"));
                test.setOccupation(jo2.getStr("occupation"));
                test.setSummarize(jo2.getStr("summarize"));
                test.setDescc(jo2.getStr("desc"));
                test.setCharacteristic(jo2.getStr("characteristic"));

                //7.修改测试的结果
                dao.update(test);

                return test.toString();
            }catch (Exception e){
                e.printStackTrace();
                return e.getMessage();
            }
        }
        return "结束测试失败！";
    }

    /**
     * 查询我的测试
     * @param uid 用户ID*/
    @Override
    public List<UserTest> queryByUid(Integer uid) {
        return dao.selectByUid(uid);
    }
}
